package com.ibm.news.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.news.beans.News;
import com.ibm.news.dao.NewsDAO;

public class NewsServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		String flag = request.getParameter("flag");	

		NewsDAO newsDAO = new NewsDAO();

		ArrayList<News> listNews = new ArrayList<News>();
		
		News news = null;
		if ("selectAll".equals(flag)) {
			listNews = newsDAO.getNewsAll();

			request.setAttribute("list", listNews);
			request.getRequestDispatcher("/bNews/selectNews.jsp").forward(request, response);
		} else if("byBigSort".equals(flag)) {
			String bigSort = request.getParameter("bigSort");
			System.out.println(bigSort);
			listNews = newsDAO.getNewsByBigSort(bigSort);
			request.setAttribute("list", listNews);
			request.getRequestDispatcher("/bNews/selectNews.jsp").forward(request, response);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);	
	}

}
